import requests
import pymongo
import threading
import time

client = pymongo.MongoClient()
db = client.douban
col_movie = db.movies
col_casts = db.casts

proxies = {
  # "http": "http://110.73.1.125:8123",
  "https": "https://175.10.86.232:29235",
}

def get_cast(id):
    print('fetching', id)
    try:
        url = 'https://api.douban.com/v2/movie/celebrity/' + str(id)
        print(url)
        req = requests.get(url, proxies=proxies)
        print('status:', req.status_code)
        if req.status_code != 200:
            print(req.json())
        data = req.json()
        print('inserting', data['name'])
        col_casts.update_one({'id': data['id']}, {'$set': data}, upsert=True)
        print('finish', data['name'])
    except:
        print(id, 'error!!!!!')

for movie in col_movie.find():
    casts = movie['casts']
    print(movie['title'])
    for cast in casts:
        threading.Thread(target=get_cast, args=(cast['id'],)).start()
        # get_cast(cast['id'])
        # time.sleep(1.5)
    print('======================')
